// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
#include "pch.h"
#include "LocatorAgent.h"

using namespace concurrency;
using namespace std;
using namespace Weathr;
using namespace Weathr::Web;

LocatorAgent::LocatorAgent(shared_ptr<ILocationService> locationService) : m_locationService(locationService)
{
    m_processBuffer = make_unique<call<tuple<float, float>>>([this](tuple<float, float> location)
    {
        m_locationService->GetLocationAsync(get<0>(location), get<1>(location)).then([this](task<shared_ptr<Location>> result)
        {
            try
            {
                send(m_outputBuffer, result.get());
            }
            catch (const Web::WebException& e)
            {
                send(m_errorBuffer, e);
            }
        });
    });
}

void LocatorAgent::QueueLocationRequest(float latitude, float longitude)
{
    send(m_requestBuffer, make_tuple(latitude, longitude));
}

void LocatorAgent::run()
{
    m_requestBuffer.link_target(m_processBuffer.get());
}